package com.jx.springcloud.controller;

import cn.hutool.core.util.IdUtil;
import com.jx.springcloud.service.ComsumerService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@DefaultProperties(defaultFallback = "Handle")
public class ConsumerControl {
    @Resource
    private ComsumerService comsumerService;

    @GetMapping(value = "/consumer/paymentOK")
    @HystrixCommand
    public String paymentOK(){
        return comsumerService.paymentOK();
    }

    @HystrixCommand(fallbackMethod = "paymentlayerHandle",commandProperties={
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",
                    value = "1500")
    })
    @GetMapping(value = "/consumer/paymentlayer")
    public String paymentlayer(){
        return comsumerService.paymentlayer();
    }

    //服务熔断测试
    @HystrixCommand(fallbackMethod = "paymentlayerHandle",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),//是否开启熔断器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),//请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),//时间窗口期
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"),//失败率达到多少时开启熔断器
    })
    @GetMapping(value = "/consumes/paymentCircuitBreaker/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id){
            if(id<0){
                throw new RuntimeException("RuntimeException---id不能为负数。 id："+id);
            }
        String simpleUUID = IdUtil.simpleUUID();
        return "调用成功，"+Thread.currentThread().getName()+"   你的流水号为："+simpleUUID;
    }

    public String Handle(){
        return "我是80调用者，服务繁忙，请稍后再试o(╥﹏╥)o";
    }

    public String paymentlayerHandle(){
        return "我是特殊定制的80调用者，服务繁忙，请稍后再试o(╥﹏╥)o";
    }

    public String paymentlayerHandle(@PathVariable("id") Integer id){
        return "我是特殊定制的80调用者，服务繁忙，请稍后再试o(╥﹏╥)o,你输入的Id为"+id;
    }
}
